package operation;

import books.BookList;

import java.util.Scanner;

public class BorrowOperation implements IOperation {
    public int search(BookList bookList) {
        System.out.println("请输入你要借阅的图书名");
        Scanner scan = new Scanner(System.in);
        String name = scan.nextLine() ;
        int len = bookList.getBookSize() ;
        for(int i = 0 ; i < len ; i ++){
            if(bookList.books[i].getName().equals(name)) {
                return i;
            }
        }
        return -1 ;
    }
    @Override
    public void work(BookList bookList) {
        System.out.println("借阅图书");
        int tmp = search(bookList) ;
        if(tmp == -1){
            System.out.println("抱歉，找不到你要借阅的书");
            return;
        }else {
            if(bookList.books[tmp].getIsBorrow() == true){
                System.out.println("抱歉，这本书已经被借走了");
            }else {
                System.out.println("借阅成功");
             bookList.books[tmp].setIsBorrow(true);
            }
        }

    }
}